package com.gl.ruanzhu.controller;


import com.github.pagehelper.PageInfo;
import com.gl.basis.common.pojo.Page;
import com.gl.ruanzhu.dto.PersonalDto;
import com.gl.ruanzhu.dto.TaskDto;
import com.gl.ruanzhu.dto.TaskIndustryDto;
import com.gl.ruanzhu.indto.TaskInDto;
import com.gl.ruanzhu.outDto.TaskOutDto;
import com.gl.ruanzhu.service.PersonalDalService;
import com.gl.ruanzhu.service.ProductDemandDalService;
import com.gl.ruanzhu.service.TaskIndustryDalService;
import com.gl.ruanzhu.util.downFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/demand")
@Api(value = "需求添加", tags = {"需求添加"})
//@Slf4j
public class ProductDemandController {


    @Autowired
    private ProductDemandDalService productDemandService;

    @Autowired
    private PersonalDalService personalDalService;

    @Autowired
    private TaskIndustryDalService taskIndustryDalService;

    @ApiOperation(value = "需求添加接口")
    @PostMapping("/addTaskDemand")

    public Page addTaskDemand(@RequestBody TaskDto taskDto)  {
        Page page = new Page<>();
        productDemandService.saveTask(taskDto);
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }

    @ApiOperation(value = "需求列表接口")
    @PostMapping("/taskDemandList")

    public Page taskDemandList(@RequestBody TaskInDto taskDto)  {
        Page page = new Page<>();
        PageInfo<TaskOutDto> taskDtoPageInfo = productDemandService.taskList(taskDto);
        page.setTotalSize(taskDtoPageInfo.getTotal());
        page.setPageSize(taskDtoPageInfo.getPageSize());
        page.setPageNo(taskDtoPageInfo.getPageNum());
        page.setResult(taskDtoPageInfo);
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }


    @ApiOperation(value = "需求详情接口")
    @PostMapping("/taskDetails")

    public Page taskDetails(@RequestBody TaskInDto taskInDto)  {
        Page page = new Page<>();
        TaskOutDto taskDtox = productDemandService.taskDetails(taskInDto.getId());
        page.setResult(taskDtox);
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }

    @ApiOperation(value = "查询负责人")
    @PostMapping("/getPersonal")
    public Page getPersonal()  {
        Page page = new Page<>();
        List<PersonalDto> personalDtos = personalDalService.quaryAll();
        page.setMsg("成功");
        page.setCode("200");

        page.setResult(personalDtos);
        return page;
    }

    @ApiOperation(value = "行业查询")
    @PostMapping("/getIndustry")
    public Page getIndustry()  {
        Page page = new Page<>();
        List<TaskIndustryDto> taskIndustryDtos = taskIndustryDalService.quaryList();
        page.setMsg("成功");
        page.setCode("200");
        page.setResult(taskIndustryDtos);
        return page;
    }


    @ApiOperation(value = "修改需求")
    @PostMapping("/modifyRequirements")
    public Page modifyRequirements(@RequestBody TaskDto taskDto)  {
        Page page = new Page<>();
        productDemandService.updateTask(taskDto);
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }


    @ApiOperation(value = "完成任务")
    @PostMapping("/finishTheTask")
    public Page finishTheTask(@RequestBody TaskDto taskDto)  {
        Page page = new Page<>();
        productDemandService.updateTaskState(taskDto);
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }



    @ApiOperation(value = "添加子任务")
    @PostMapping("/AddChildTask")
    public Page AddChildTask(@RequestBody TaskDto taskDto )  {
        Page page = new Page<>();
        productDemandService.saveChildTask(taskDto);
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }

    @ApiOperation(value = "删除数据")
    @PostMapping("/delById")
    public Page delById(@RequestBody TaskInDto taskDto )  {
        Page page = new Page<>();
        productDemandService.delById(taskDto.getId());
        page.setMsg("成功");
        page.setCode("200");
        return page;
    }
//
//    @ApiOperation(value = "添加子任务")
//    @PostMapping("/AddChildTask")
//    public Page AddChildTask(@RequestBody TaskDto taskDto, MultipartFile file)  {
//        Page page = new Page<>();
//        productDemandService.saveChildTask(taskDto,file);
//        page.setMsg("成功");
//        page.setCode("200");
//        return page;
//    }

    @ApiOperation(value = "打包下载接口")
    @PostMapping("/downloadFile")
    public void downloadFile(@RequestBody TaskInDto taskInDto, HttpServletResponse response, HttpServletRequest request) throws IOException, NoSuchAlgorithmException {
        Page page = new Page<>();
        TaskOutDto taskDtox = productDemandService.taskDetails(taskInDto.getId());
        taskDtox.getEnclosureUrl();
        downFileUtil.download(taskDtox.getEnclosureUrl(), taskDtox.getEnclosureName(), response);

    }
}
